﻿/**************************************************************************
创建时间:	2020/5/13 8:56:45    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

描	述：Dockpanel 的 MDI 母窗体（默认其它窗体都为dockpanel的子窗体）
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using ZhCun.Utils.Helpers;

namespace ZhCun.Win
{
    public partial class FrmMDI : FrmBase
    {
        public FrmMDI()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 当前已经打开的MDI子窗体
        /// </summary>
        protected readonly Dictionary<Type, FrmBase> FormList = new Dictionary<Type, FrmBase>();
        /// <summary>
        /// 当前已经打开的MDI子窗体,来自Spring
        /// </summary>
        private readonly Dictionary<string, FrmBase> FormNameList = new Dictionary<string, FrmBase>();
        /// <summary>
        /// 获取 dockpanel 已打开的form对象
        /// </summary>
        public T GetTabForm<T>()
            where T : Control
        {
            return FormList[typeof(T)] as T;
        }
        /// <summary>
        /// tab选项卡方式显示窗体
        /// </summary>
        protected T ShowTabForm<T>(DockPanel dockPanel)
            where T : Control, new()
        {
            var frm = CreateSingletonForm<T>();
            frm.Show(dockPanel);
            return frm as T;
        }
        /// <summary>
        /// tab选项卡方式显示窗体
        /// </summary>
        protected T ShowTabForm<T>(DockPanel dockPanel, DockState state)
            where T : Control, new()
        {
            var frm = CreateSingletonForm<T>();
            frm.Show(dockPanel, state);
            return frm as T;
        }
        /// <summary>
        /// spring 创建 tab选项卡方式显示窗体
        /// </summary>
        protected void ShowTabForm(string name, DockPanel dockPanel, string formText = null)
        {
            var frm = CreateSingletonForm(name);
            if (!string.IsNullOrWhiteSpace(formText))
            {
                frm.Text = formText;
            }
            frm.Show(dockPanel);
        }
        /// <summary>
        /// 创建一个 DockContent 对象
        /// </summary>
        private FrmBase CreateDockForm(object control, Action FormCloseHandle)
        {
            FrmBase frm;
            if (control is FrmBase)
            {
                frm = control as FrmBase;
            }
            else if (control is UserControl ucControl)
            {
                frm = new FrmBase();
                ucControl.Parent = frm;
                ucControl.Dock = DockStyle.Fill;
                frm.Text = ucControl.Tag?.ToString();
            }
            else
            {
                throw new NotImplementedException("未实现的单例窗体类型");
            }
            frm.FormClosed += new FormClosedEventHandler(delegate (object sender, FormClosedEventArgs e)
            {
                //关闭时从字典列表中删除
                FormCloseHandle();
            });
            return frm;
        }
        /// <summary>
        /// 创建单例窗体
        /// </summary>
        /// <typeparam name="T">DockContent 或者 UcBaseDockControl</typeparam>
        /// <returns>返回一个单例窗体</returns>
        public FrmBase CreateSingletonForm<T>() where T : Control, new()
        {
            Type controlType = typeof(T);
            if (!FormList.ContainsKey(controlType))
            {
                T t = new T();
                var frm = CreateDockForm(t, () => FormList.Remove(controlType));
                FormList.Add(controlType, frm);
                return frm;
            }
            return FormList[controlType];
        }
        /// <summary>
        /// 从spring实例中创建单例窗口，spring需要设置未非单例模式，由这里实现单例模式
        /// </summary>
        /// <param name="name">服务实例名</param>
        /// <returns>返回实例对象</returns>
        public FrmBase CreateSingletonForm(string name)
        {
            if (!FormNameList.ContainsKey(name))
            {
                var obj = CreateForm(name);
                var frm = CreateDockForm(obj, () => FormNameList.Remove(name));
                FormNameList.Add(name, frm);
                return frm;
            }
            return FormNameList[name];
        }
        
        private void FrmMDI_Load(object sender, EventArgs e)
        {
            IsMdiContainer = true;
        }
    }
}